package com.ygqh.baby.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.OrderStatus;
import com.ygqh.baby.exception.YgHttpException;
import com.ygqh.baby.http.YgRequestExecutor;
import com.ygqh.baby.http.YgResponse;
import com.ygqh.baby.po.YgOrder;
import com.ygqh.baby.service.YgOrderService;
import com.ygqh.baby.utils.DateConvertUtils;
import com.ygqh.baby.utils.ExceptionUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SyncOrderHandler {
	protected final Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	private YgOrderService ygOrderService;

	private final static String api_uri = "";

	public void syncOrderToErp() {
		Date endDate = new Date();
		Date startDate = DateConvertUtils.addMinute(endDate, -10);
		List<YgOrder> orderList = ygOrderService.findByPaidTimeHasDetail(startDate, endDate, null, OrderStatus.WaitingCheck, OrderStatus.WaitingSend);

		if (CollectionUtils.isEmpty(orderList)) {
			return;
		}
		String method = "originalOrderPush";
		Map<String, String> data = new HashMap<String, String>();
		data.put("method", method);
		data.put("orderList", JSON.toJSONString(orderList));
		Message msg = null;
		try {
			YgRequestExecutor request = new YgRequestExecutor();
			YgResponse response = request.post(api_uri, data);
			msg = response.getAsObject(new TypeReference<Message>() {
			});
		} catch (YgHttpException e) {

			logger.error("method=" + method + ";" + ExceptionUtil.getExceptionMsg(e));
			msg = Message.error(ExceptionUtil.getExceptionMsg(e), e);
			e.printStackTrace();
		}
		if (msg.getType().equals(Message.Type.error)) {
		}

	}
}
